###########################################################################
# Lipps & Sczepanski & Malet
# Understanding Preferences over Borders
# International Studies Quaterly

# Appendix A: Descriptive Statistics --------------------------------------
###########################################################################

rm(list = ls())

packages <- c("tidyverse", "stringr", "tidytext", "ggplot2", "xtable")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

library(tidyverse)
library(ggplot2)
library(stringr)
library(xtable)
library(viridis)

#Load data
dat <- read.csv("survey_dat_clean.csv", na.strings=c("","NA"))

#### TABLES AND FIGURES ####--------------------------------------------------------


#Table B.1: list of parties in the German Bundestag

#Figure B.1: German parties on the CHES left-right scale
ches <- read.csv("1999-2019_CHES.csv")
ches <- ches %>% filter(year==2019 & country==3) %>%
  dplyr::select(party, lrecon, galtan) %>%
  filter(party!="DieTier") %>%
  filter(party!="Piraten")

ches$party <- ordered(ches$party, levels = c("LINKE", "SPD", "GRUNEN", "CDU", "FDP", "CSU", "AfD"))

pdf(file = "fig_B1.pdf",width = 8,height = 5,onefile=F)

ggplot(ches, aes(x=lrecon, y=galtan, color=party)) +
  geom_point(size=5) +
  labs(y="GAL-TAN", x="Left-Right") +
  scale_color_manual(values=c("violetred1", "red3", "forestgreen", "black", "darkgoldenrod1", "darkblue", "deepskyblue3"), name="")+
  theme_bw() +
  theme(
    axis.text.y = element_text(size = 12),
    axis.text.x = element_text(size=12),
    axis.title.y= element_text(size = 14),
    axis.title.x = element_text(size=14),
    legend.position = "top",
    legend.text = element_text(size=12))

dev.off()

#Figure B.2: Respondents' reported vote intention
pie <- as.data.frame(table(dat$vote_intention)) #compared to seat distribution in Bundestag: SPD and FDP slightly underrepresented, AfD and Greens overrepresented

pie$Var1 <- ordered(pie$Var1, levels = c("The Left", "SPD", "Greens", "CDU/CSU", "FDP", "AfD", "Other", "Don't know", "No show"))

pdf(file = "fig_B2.pdf",width = 8,height = 5,onefile=F)

ggplot(pie, aes(x="", y=Freq, fill=Var1)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y") +
  labs(y="", x="") +
  theme_void() + 
  theme(legend.position="right",
        legend.text = element_text(size=12)) +
  scale_fill_manual(values=c("violetred1", "red3", "forestgreen", "black", "darkgoldenrod1", "deepskyblue3", "darkgrey", "darkgrey", "darkgrey"), name="") +
  geom_text(aes(label = Freq), position = position_stack(vjust = 0.5), color = "white", size=5)
  
dev.off()

#Figure B.3: Answer to openness question by vote intention
dat$vote_intention <- ordered(dat$vote_intention, levels = c("The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD", "Other", "No show", "Don't know"))

pdf(file = "fig_B3.pdf",width = 8,height = 5,onefile=F)

ggplot(dat, aes(x=border_open_close_1, y=vote_intention)) +
  geom_violin(aes(color = vote_intention, fill=vote_intention)) +
  scale_color_manual(values=c("violetred1", "red3", "forestgreen", "darkgoldenrod1", "black", "deepskyblue3", "darkgrey", "darkgrey", "darkgrey"))+
  scale_fill_manual(values=c("violetred1", "red3", "forestgreen", "darkgoldenrod1", "black", "deepskyblue3", "darkgrey", "darkgrey", "darkgrey")) +
  stat_summary(fun.y="mean", geom="point", color="white", size=3) +
  stat_summary(fun.data="mean_cl_normal", color="white", geom="errorbar",  size=1.5) +
  scale_y_discrete(limits=rev) +
  scale_x_continuous(breaks=c(0,1,2,3,4,5,6,7,8,9,10)) +
  labs(x="Border should be: 0=completely open to 10=completely closed", y="") +
  guides(fill=guide_legend(title="Vote intention"), color="none") +
  theme_minimal() +
  theme(
      axis.text.y = element_text(size = 12),
      axis.text.x = element_text(size=12),
      axis.title.y= element_text(size = 14),
      axis.title.x = element_text(size=14),
      legend.position = "right",
      legend.text = element_text(size=12))

dev.off()
